package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.LiuxuequanAddCommentResponse;
import com.wephone.Student.bean.LiuxuequanSingleDetailResponse;
import com.wephone.Student.bean.data.LiuxuequanCommentData;
import com.wephone.Student.bean.data.LiuxuequanInfoData;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.PullToRefreshView;
import com.wephone.Student.util.PullToRefreshView.OnFooterRefreshListener;
import com.wephone.Student.util.PullToRefreshView.OnHeaderRefreshListener;
import com.wephone.Student.util.RoundImageView;

public class LiuXueQuanPinglunDetailActivity extends BaseActivity {
	private ListView listView;
	private ListViewAdapter adapter;
	private LiuxuequanInfoData infoData;
	private View listHeadView;
	private EditText addCommentEdit;
	private LiuxuequanCommentData delCommentData;
	private PullToRefreshView pullToRefreshListView;
	
	//delete
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanPinglunDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					finish();
					Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler1(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanPinglunDetailActivity.this,LiuxuequanSingleDetailResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					LiuxuequanSingleDetailResponse res = (LiuxuequanSingleDetailResponse)response;
					infoData = res.getData();
					if(infoData!=null){
						refreshView();
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler5(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanPinglunDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					setResult(11);
					Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
					addCommentEdit.getText().clear();
					InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(addCommentEdit.getWindowToken(), 0);

					RequestParams params = new RequestParams();
			 		params.put("id", infoData.getId());
					HttpClient.getInstance().get(Constants.API_GET_STUDY_CIRCLE_DETAIL_URL, params, getHandler1());
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler7(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanPinglunDetailActivity.this,LiuxuequanAddCommentResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					addCommentEdit.getText().clear();
					InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(addCommentEdit.getWindowToken(), 0);
					LiuxuequanAddCommentResponse res = (LiuxuequanAddCommentResponse)response;
					if(res!=null && res.getData()!=null){
//						if(infoData!=null){
//							if(infoData.getComment()==null){
//								List<LiuxuequanCommentData> commentDataList = new ArrayList<LiuxuequanCommentData>();
//								commentDataList.add(res.getData());
//								infoData.setComment(commentDataList);
//							}else{
//								infoData.getComment().add(res.getData());
//							}
							adapter.getResults().add(res.getData());
							adapter.notifyDataSetChanged();
							listView.smoothScrollToPosition(adapter.getCount());
							Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
//						}
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler8(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanPinglunDetailActivity.this,LiuxuequanAddCommentResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
//					LiuxuequanAddCommentResponse res = (LiuxuequanAddCommentResponse)response;
//					if(infoData!=null){
						adapter.getResults().remove(delCommentData);
						adapter.notifyDataSetChanged();
						Toast.makeText(getApplicationContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
//					}
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_liuxuequan_pinglun_detail);
		
		init();
	}
	
	private void refreshView(){
		initListHeaderView();
		if(infoData.getComment()!=null && infoData.getComment().size()>0){
			adapter = new ListViewAdapter(infoData.getComment());
		}else{
			adapter = new ListViewAdapter(new ArrayList<LiuxuequanCommentData>());
		}
		listView.setAdapter(adapter);
	}
	
	private void init(){
		listView = (ListView)findViewById(R.id.listview);
		addCommentEdit = (EditText)findViewById(R.id.add_comment_edit);
		
		listView.setOnItemClickListener(new ListView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if(arg2==0){
					return;
				}
				LiuxuequanCommentData commentData = infoData.getComment().get(arg2-1);
				showAddCommentDialog("回复"+commentData.getuInfo().getuName(),commentData.getId(),commentData.getSid());
			}});
		listView.setOnItemLongClickListener(new ListView.OnItemLongClickListener(){
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg2==0){
					return true;
				};
				delCommentData = infoData.getComment().get(arg2-1);
				if(BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(delCommentData.getuInfo().getuId())){
					new AlertDialog.Builder(LiuXueQuanPinglunDetailActivity.this)
					.setTitle("提示")
					.setMessage("您确定要删除该回复吗?")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							RequestParams params = new RequestParams();
							params.put("id", delCommentData.getId());
							params.put("uid", delCommentData.getUid());
							HttpClient.getInstance().get(Constants.API_DELETE_STUDY_CIRCLE_REPLY_URL, params, getHandler8());
						}
					})
					.setNegativeButton("取消", null).show();
				}
				return true;
			}});
		
		pullToRefreshListView = (PullToRefreshView)findViewById(R.id.pull_refresh_listview);
		pullToRefreshListView.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
			@Override
			public void onHeaderRefresh(PullToRefreshView view) {
				adapter.getResults().clear();
				adapter.notifyDataSetChanged();
				
				RequestParams params = new RequestParams();
		 		params.put("id", infoData.getId());
				HttpClient.getInstance().get(Constants.API_GET_STUDY_CIRCLE_DETAIL_URL, params, getHandler1());
			}
		});
		pullToRefreshListView.setOnFooterRefreshListener(new OnFooterRefreshListener() {
			@Override
			public void onFooterRefresh(PullToRefreshView view) {
				pullToRefreshListView.onFooterRefreshComplete();
			}
		});
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.btn_add_comment).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String content = addCommentEdit.getText().toString();
				if(!TextUtils.isEmpty(content)){
					RequestParams params = new RequestParams();
					params.put("sid", infoData.getId());
					params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("content", content);
					HttpClient.getInstance().post(Constants.API_ZHIDAO_ADD_STUDY_CIRCLE_REPLY_URL, params, getHandler7());
				}
			}
		});
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			infoData = (LiuxuequanInfoData)bundle.getSerializable("infoData");
			
			if(infoData!=null){
				listHeadView = getLayoutInflater().inflate(R.layout.include_liuxuequan_pinglun_detail_header, null);
				initListHeaderView();
				listView.addHeaderView(listHeadView);
				if(infoData.getComment()!=null && infoData.getComment().size()>0){
					adapter = new ListViewAdapter(infoData.getComment());
				}else{
					adapter = new ListViewAdapter(new ArrayList<LiuxuequanCommentData>());
				}
				listView.setAdapter(adapter);
			}
		}
	}
	
	private void initListHeaderView(){
		LiuxuequanInfoData data = infoData;
		TextView itemName = (TextView)listHeadView.findViewById(R.id.item_name);
		itemName.setText(data.getuInfo().getuName());
		TextView itemContent = (TextView)listHeadView.findViewById(R.id.item_content);
		if(!TextUtils.isEmpty(data.getContent())){
			itemContent.setVisibility(View.VISIBLE);
			itemContent.setText(data.getContent());
		}else{
			itemContent.setVisibility(View.GONE);
		}
		TextView itemTime = (TextView)listHeadView.findViewById(R.id.item_create_time);
		itemTime.setText(sdf.format(new Date(Long.parseLong(data.getCtime())*1000)));
		
		if(BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(infoData.getuInfo().getuId())){
			listHeadView.findViewById(R.id.item_delete_liuxuequan).setVisibility(View.VISIBLE);
			listHeadView.findViewById(R.id.item_delete_liuxuequan).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					new AlertDialog.Builder(LiuXueQuanPinglunDetailActivity.this)
					.setTitle("提示")
					.setMessage("您确定要删除该留学圈信息吗?")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							RequestParams params = new RequestParams();
							params.put("id", infoData.getId());
							params.put("uid", infoData.getUid());
							HttpClient.getInstance().get(Constants.API_DELETE_STUDY_CIRCLE_URL, params, getHandler());
						}
					})
					.setNegativeButton("取消", null).show();
				}
			});
		}
		
		LinearLayout zanLayout = (LinearLayout)listHeadView.findViewById(R.id.zan_user_logo_layout);
		zanLayout.removeAllViews();
		if(infoData.getSupportName()==null || infoData.getSupportName().size()==0){
			listHeadView.findViewById(R.id.zan_group_layout).setVisibility(View.GONE);
		}else{
			for(UserInfoData userInfo:infoData.getSupportName()){
				View zanLogoView = getLayoutInflater().inflate(R.layout.include_liuxuequan_pinglun_zan_user_logo, null);
				RoundImageView itemImage = (RoundImageView)zanLogoView.findViewById(R.id.item_logo_image);
				if(!TextUtils.isEmpty(userInfo.getuIcon())){
					String endStr = userInfo.getuIcon().substring(userInfo.getuIcon().lastIndexOf("."));
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userInfo.getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
				}else{
					itemImage.setImageResource(R.drawable.default_user_logo);
				}
				itemImage.setTag(userInfo);
				itemImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						UserInfoData userInfo = (UserInfoData)v.getTag();
						Bundle bundle = new Bundle();
						bundle.putSerializable("userData", userInfo);
						startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
					}
				});
				zanLayout.addView(zanLogoView);
			}
		}
		
		RoundImageView itemImage = (RoundImageView)listHeadView.findViewById(R.id.item_logo_image);
		if(!TextUtils.isEmpty(data.getuInfo().getuIcon())){
			String endStr = data.getuInfo().getuIcon().substring(data.getuInfo().getuIcon().lastIndexOf("."));
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
		}else{
			itemImage.setImageResource(R.drawable.default_user_logo);
		}
		itemImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("userData", infoData.getuInfo());
				startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
			}
		});
		if(data.getImg()!=null){
			if(data.getImg().length>0){
				ImageView itemAddImage = (ImageView)listHeadView.findViewById(R.id.item_add_image_1);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[0], itemAddImage, options);
				itemAddImage.setVisibility(View.VISIBLE);
				if(data.getBigimg()!=null && data.getBigimg().length>0){
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getBigimg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}else{
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getImg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}
				itemAddImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = arg0.getTag().toString();
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}else{
				listHeadView.findViewById(R.id.item_add_image_1).setVisibility(View.GONE);
			}
			if(data.getImg().length>1){
				ImageView itemAddImage = (ImageView)listHeadView.findViewById(R.id.item_add_image_2);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[1], itemAddImage, options);
				itemAddImage.setVisibility(View.VISIBLE);
				if(data.getBigimg()!=null && data.getBigimg().length>0){
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getBigimg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}else{
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getImg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}
				itemAddImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = arg0.getTag().toString();
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}else{
				listHeadView.findViewById(R.id.item_add_image_2).setVisibility(View.GONE);
			}
			if(data.getImg().length>2){
				ImageView itemAddImage = (ImageView)listHeadView.findViewById(R.id.item_add_image_3);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[2], itemAddImage, options);
				itemAddImage.setVisibility(View.VISIBLE);
				if(data.getBigimg()!=null && data.getBigimg().length>0){
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getBigimg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}else{
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getImg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}
				itemAddImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = arg0.getTag().toString();
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}else{
				listHeadView.findViewById(R.id.item_add_image_3).setVisibility(View.GONE);
			}
			if(data.getImg().length>3){
				ImageView itemAddImage = (ImageView)listHeadView.findViewById(R.id.item_add_image_4);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[3], itemAddImage, options);
				itemAddImage.setVisibility(View.VISIBLE);
				if(data.getBigimg()!=null && data.getBigimg().length>0){
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getBigimg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}else{
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getImg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}
				itemAddImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = arg0.getTag().toString();
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}else{
				listHeadView.findViewById(R.id.item_add_image_4).setVisibility(View.GONE);
			}
			if(data.getImg().length>4){
				ImageView itemAddImage = (ImageView)listHeadView.findViewById(R.id.item_add_image_5);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[4], itemAddImage, options);
				itemAddImage.setVisibility(View.VISIBLE);
				if(data.getBigimg()!=null && data.getBigimg().length>0){
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getBigimg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}else{
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getImg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}
				itemAddImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = arg0.getTag().toString();
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}else{
				listHeadView.findViewById(R.id.item_add_image_5).setVisibility(View.GONE);
			}
			if(data.getImg().length>5){
				ImageView itemAddImage = (ImageView)listHeadView.findViewById(R.id.item_add_image_6);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg()[5], itemAddImage, options);
				itemAddImage.setVisibility(View.VISIBLE);
				if(data.getBigimg()!=null && data.getBigimg().length>0){
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getBigimg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}else{
					StringBuffer sb = new StringBuffer();
					for(String itm:data.getImg()){
						if(!TextUtils.isEmpty(itm)){
							if(itm.contains(Constants.IMAGE_PHOTO_SERVER_PATH)){
								sb.append(itm).append(",");
							}else{
								sb.append(Constants.IMAGE_PHOTO_SERVER_PATH+itm).append(",");
							}
						}
					}
					if(sb.length()>0){
						sb.deleteCharAt(sb.length()-1);
					}
					itemAddImage.setTag(sb.toString());
				}
				itemAddImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = arg0.getTag().toString();
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}else{
				listHeadView.findViewById(R.id.item_add_image_6).setVisibility(View.GONE);
			}
		}
	}
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	private DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.liuxuequan_image_default)
    .showImageOnFail(R.drawable.liuxuequan_image_default)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
    .build();
	
	private class ListViewAdapter extends BaseAdapter{
		private List<LiuxuequanCommentData> results;
		
		public ListViewAdapter(List<LiuxuequanCommentData> results) {
			super();
			this.results = results;
		}
		
		public List<LiuxuequanCommentData> getResults() {
			return results;
		}

		@Override
		public int getCount() {
			return results.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView = getLayoutInflater().inflate(R.layout.include_liuxuequan_pinglun_list_item, null);
			}
			LiuxuequanCommentData data = results.get(position);
			if(position==0){
				convertView.findViewById(R.id.item_comment_icon_image).setVisibility(View.VISIBLE);
			}else{
				convertView.findViewById(R.id.item_comment_icon_image).setVisibility(View.INVISIBLE);
			}
			TextView itemName = (TextView)convertView.findViewById(R.id.item_name);
			if(data.getReplyuInfo()!=null){
				SpannableStringBuilder style=new SpannableStringBuilder(data.getuInfo().getuName()+"回复"+data.getReplyuInfo().getuName());
			    style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.default_gray)),data.getuInfo().getuName().length(),data.getuInfo().getuName().length()+2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);              
			    itemName.setText(style);
			}else{
				itemName.setText(data.getuInfo().getuName());
			}
			
			TextView itemContent = (TextView)convertView.findViewById(R.id.item_content);
			itemContent.setText(data.getContent());
			TextView itemTime = (TextView)convertView.findViewById(R.id.item_create_time);
			itemTime.setText(sdf.format(new Date(Long.parseLong(data.getCtime())*1000)));
			RoundImageView itemImage = (RoundImageView)convertView.findViewById(R.id.item_logo_image);
			if(!TextUtils.isEmpty(data.getuInfo().getuIcon())){
				String endStr = data.getuInfo().getuIcon().substring(data.getuInfo().getuIcon().lastIndexOf("."));
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
			}else{
				itemImage.setImageResource(R.drawable.default_user_logo);
			}
			itemImage.setTag(data);
			itemImage.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					LiuxuequanCommentData data = (LiuxuequanCommentData)v.getTag();
					Bundle bundle = new Bundle();
					bundle.putSerializable("userData", data.getuInfo());
					startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
				}
			});
			return convertView;
		}		
	}
	
	private void showAddCommentDialog(String dialogTitle,final String replyId,final String sid){
		final View inputView = getLayoutInflater().inflate(R.layout.dialog_liuxuequan_add_comment_view, null);  
		AlertDialog.Builder builder = new AlertDialog.Builder(LiuXueQuanPinglunDetailActivity.this);
		if(!TextUtils.isEmpty(dialogTitle)){
			builder.setTitle(dialogTitle);
		}else{
			builder.setTitle("添加评论");
		}
		builder.setView(inputView);  
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				String content = ((EditText)inputView.findViewById(R.id.dialog_add_comment_input)).getText().toString().trim().replace("\n", "");
				if(content.length()>0){
					RequestParams params = new RequestParams();
					params.put("sid", sid);
					params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("content", content);
					if(!TextUtils.isEmpty(replyId)){
						params.put("reply_id", replyId);
					}
					HttpClient.getInstance().post(Constants.API_ZHIDAO_ADD_STUDY_CIRCLE_REPLY_URL, params, getHandler7());
				}else{
					Toast.makeText(getApplicationContext(), "评论内容不能为空", Toast.LENGTH_SHORT).show();
				}
			}
		});  
		builder.setPositiveButton("取消", null); 
		builder.show();  
	}

}
